package com.at.bigdata.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.expressions.Aggregator
import org.apache.spark.sql.{Encoder, Encoders, SaveMode, SparkSession}

/**
 *
 * @author cdhuangchao3
 * @date 2023/5/27 8:05 PM
 */
object Spark04_SparkSql_Jdbc {

  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("operator")
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()
    import spark.implicits._

    val df = spark.read.format("jdbc")
      .option("url", "jdbc:mysql://11.158.108.205:3306/hc_test?serverTimezone=GMT%2B8&useSSL=false")
      .option("driver", "com.mysql.cj.jdbc.Driver")
      .option("user", "root")
      .option("password", "Jdrc@123456")
      .option("dbtable", "user")
      .load()

//    df.show()

    // 保存数据
    df.write.format("jdbc")
      .option("url", "jdbc:mysql://11.158.108.205:3306/hc_test?serverTimezone=GMT%2B8&useSSL=false")
      .option("driver", "com.mysql.cj.jdbc.Driver")
      .option("user", "root")
      .option("password", "Jdrc@123456")
      .option("dbtable", "user1")
      .mode(SaveMode.Append)
      .save()

    spark.close();
  }

}
